<?php
class Index extends Action {
	private $limit = 15;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		
		$start = rqst ( 'start', 1 ); // 分页
		$where = Model::where_build ( array ('uid', 'gname', 'account like', 'email like', 'status' ), $data, array ('account' => 'like', 'email' => 'like' ) );
		
		$userModel = new VipUserModel ();
		$userModel->alias ( 'U' );
		$userModel->join ( "core_group AS CG", 'U.gname = CG.gname' );
		$userModel->sort ( array ('uid', 'd' ) );
		
		$users = $userModel->where ( $where )->limit ( $this->limit, $start - 1 )->count ( true )->retrieve ( "U.*,CG.name as group_name" );
		
		$data ['limit'] = $this->limit;
		
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		
		if ($users) {
			$data ['items'] = $users;
			$data ['totalCount'] = $users->countTotal;
		}
		
		$data ['stas'] = array ('new' => '<span class="label">未激活</span>', 'actived' => '<span class="label label-success">正常</span>', 'inactived' => '<span class="label label-warning">禁用</span>' );
		$gM = new CoreGroupModel ();
		$groups = $gM->where ( array ('type' => 'vip' ) )->select ();
		$data ['groups'] = $groups;
		
		if ($groups) {
			$data ['group_options'] = $groups->toArray ( 'gname', 'name', array ('' => '请选择会员组' ) );
		} else {
			$data ['group_options'] = array ('' => '请选择会员组' );
		}
		
		bind ( 'get_vip_options', array ($this, 'get_user_options' ), 10, 2 );
		bind ( 'get_vip_bench_options', array ($this, 'get_user_bench_options' ) );
		return new SmartyView ( $data, 'vip/Index' );
	}
	public function get_user_options($options, $user) {
		$options .= '<a title="编辑" href="./?Ctlr=EditVip&uid=' . $user ['uid'] . '"><i class="icon-edit"></i>编辑</a>';
		if ($user ['status'] == 'actived') {
			$options .= '<a class="torg" title="禁用" onclick="return confirm(\'确定要禁用该会员账户?\');" href="./?Ctlr=ActiveVip&status=inactived&uid=' . $user ['uid'] . '"><i class="icon-ban-circle"></i>禁用</a>';
		} else {
			$options .= '<a class="tgre" title="激活" onclick="return confirm(\'确定要激活该会员账户?\');" href="./?Ctlr=ActiveVip&status=actived&uid=' . $user ['uid'] . '"><i class="icon-ok-circle"></i>激活</a>';
		}		
		$options .= '<a class="tred" title="删除" onclick="return confirm(\'确定要删除该会员账户?\');" href="./?Ctlr=DelVip&uid=' . $user ['uid'] . '"><i class="icon-trash"></i>删除</a>';
		if(icando('grant','user')){
			$options .= '<a title="授权" data-content="' . $user ['account'] . '" class="grant-user" id="grant-VIP-' . $user ['uid'] . '" href="' . ADMIN_URL . 'dashboard/?Ctlr=Grant&type=VIP&uid=' . $user ['uid'] . '"><i class="icon-user"></i>授权</a>';
		}
		return $options;
	}
	public function get_user_bench_options($options) {
		$options .= '<li><a href="./?Ctlr=DelVip" id="menu-del-user"><i class="icon-trash"></i>删除</a></li>';
		$options .= '<li><a href="./?Ctlr=ActiveVip&status=actived" class="menu-active-user"><i class="icon-pencil"></i>激活</a></li>';
		$options .= '<li><a href="./?Ctlr=ActiveVip&status=inactived" class="menu-active-user"><i class="icon-ban-circle"></i>禁用</a></li>';
		return $options;
	}
}